package where2go.sctu.com.zhiyou.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.adpater.DynamicAdapter;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.Dynamic;
import where2go.sctu.com.zhiyou.entity.User;

/**
 * ProjectName: zhiyou
 * Created by 张某人
 * CreatedTime:on 2018/4/25   23:02
 * Description: 动态fragment
 */

public class DynamicFragment extends Fragment implements View.OnClickListener {

	private RecyclerView mRecyclerView;
	private ImageView personImg,editImg;
	private DynamicAdapter dynamicAdapter;

	private String url="http://47.104.80.155/youyiyou/dynamic/showAllByPage?page=1";

	private List<Dynamic.DynamicDataBean> dynamicList=new ArrayList<Dynamic.DynamicDataBean>();
	private List<User> userList=new ArrayList<User>();

	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");



	private int [] avatarArry={R.drawable.barnner1,R.drawable.barnner3,R.drawable.barnner2,
			R.drawable.barnner1,R.drawable.barnner3,R.drawable.barnner2,R.drawable.barnner1,
			R.drawable.barnner3,R.drawable.barnner2,R.drawable.barnner1};
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view= inflater.inflate(R.layout.dynamic_fragment,container,false);
		findView(view);
		return view;
	}



	private void loaderData(String url){
		StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String json) {  //onPostExecute
				Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
				Dynamic  dynamic= gson.fromJson(json, Dynamic.class);
				dynamicList=dynamic.getData();
				dynamicAdapter=new DynamicAdapter(getContext(),dynamicList);
				mRecyclerView.setAdapter(dynamicAdapter);
				dynamicAdapter.notifyDataSetChanged();
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError volleyError) {
				Log.e("TAG", volleyError.toString());
				Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
			}
//        第三部：把请求对象加入到请求的队列当中
		});
		MyApplication.getHttpQueues().add(request);
	}

	private void findView(View view) {

		mRecyclerView=(RecyclerView)view.findViewById(R.id.dynamic_recyclerView);
		personImg=(ImageView) view.findViewById(R.id.person_img);
		editImg=(ImageView)view.findViewById(R.id.dynamic_edit);
		personImg.setOnClickListener(this);
		editImg.setOnClickListener(this);

		mRecyclerView.setLayoutManager(new ScrollLinearLayoutManager(getContext(),
				ScrollLinearLayoutManager.VERTICAL,false));
		dynamicList=new ArrayList<Dynamic.DynamicDataBean>();
		loaderData(url);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.person_img:

				break;
			case R.id.dynamic_edit:

				break;
		}
	}
}
